package com.message.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.message.api.group.dao.AcGroupMapper;
import com.message.api.group.entity.AcGroup;
import com.message.api.group.service.AcGroupService;
import com.message.api.group.vo.AcGroupVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcGroupServiceImpl extends ServiceImpl<AcGroupMapper, AcGroup> implements AcGroupService {

    @Override
    public List<AcGroupVo> findByExample(AcGroupVo example) {
        return this.baseMapper.findByExample(buildWrapper(example));
    }

    @Override
    public Page<AcGroupVo> findByPage(AcGroupVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(baseMapper.findByExample(buildWrapper(example), page));
    }

    @Override
    public AcGroup findByAcgroup(String acgroup) {
        return baseMapper.findByAcgroup(acgroup);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<AcGroupVo> buildWrapper(AcGroupVo example) {
        QueryWrapper<AcGroupVo> wrapper = new QueryWrapper<>();

        /** 条件查询 */
        String  name          = example.getName();
        Integer grouptypeid   = example.getGrouptypeid();
        String  grouptypeName = example.getGrouptypeName();
        String  acgroup       = example.getAcgroup();
        wrapper.like(StringUtils.isNotEmpty(grouptypeName), "grouptype_name", grouptypeName);
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(name), AcGroupVo::getName, name)
                .eq(null != grouptypeid, AcGroupVo::getGrouptypeid, grouptypeid)

                .like(StringUtils.isNotEmpty(acgroup), AcGroupVo::getAcgroup, acgroup)
                .eq(AcGroupVo::getState, "1")
                .orderByAsc(AcGroupVo::getId);
        return wrapper;
    }

}
